<!-- 侧边栏 -->
<template>
  <div class="f-menu" :style="{ width: $store.state.asideWidth }">
    <!-- 使用handleSelect监听 -->
    <el-menu
      class="border-0"
      @select="handleSelect"
      :collapse="isCollapse"
      :collapse-transition="false"
      unique-opened
      :default-active="defaultActive"
    >
      <template v-for="(item, index) in asideMenus" :key="index">
        <!-- 判断它有没有child字段并且这个数组长度是否大于0，是的话就代表有下级菜单 index绑定name值-->
        <!-- 二级菜单 -->
        <el-sub-menu
          v-if="item.child && item.child.length > 0"
          :index="item.name"
        >
          <template #title>
            <el-icon>
              <!-- 动态图标 -->
              <component :is="item.icon" />
            </el-icon>
            <!-- 标题部分 -->
            <span>{{ item.name }}</span>
          </template>

          <el-menu-item
            v-for="(childItem, childIndex) in item.child"
            :key="childIndex"
            :index="childItem.frontpath"
          >
            <!-- 动态图标 -->
            <el-icon>
              <component :is="childItem.icon" />
            </el-icon>
            <!-- 标题部分 -->
            <span>{{ childItem.name }}</span>
          </el-menu-item>
        </el-sub-menu>
        <el-menu-item v-else :index="item.frontpath">
          <!-- 一级菜单 -->
          <el-icon>
            <!-- 动态图标 -->
            <component :is="item.icon" />
          </el-icon>
          <!-- 标题部分 -->
          <span>{{ item.name }}</span>
        </el-menu-item>
      </template>
    </el-menu>
  </div>
</template>
<script setup>
import { computed, ref, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useStore } from 'vuex'
const router = useRouter()
const store = useStore()
const route = useRoute()
console.log('Fmenu router');

console.log(route.path)
// 默认选中当前路径
const defaultActive = ref(route.path)

// 监听路由变化，更新默认激活项
watch(
  () => route.path,
  (newPath) => {
    defaultActive.value = newPath
  }
)

// 是否折叠
const isCollapse = computed(() => {
  return !(store.state.asideWidth == '200px')
})

const asideMenus = [
  {
    name: '首页',
    icon: 'HomeFilled',
    frontpath: '/'
  },
  {
    name: '对话',
    icon: 'Service',
    frontpath: '/chat'
  },
  {
    name: '绘图',
    icon: 'edit-pen',
    frontpath: '/draw'
  },
  {
    name: '知识库',
    icon: 'Notebook',
    frontpath: '/knowledge'
  },
]
const handleSelect = (e) => {
  console.log(e)
  router.push(e)
}
</script>
<style>
.f-menu {
  transition: all 0.2s;
  top: 64px;
  bottom: 0;
  left: 0;
  overflow-y: auto;
  overflow-x: hidden;
  @apply shadow-md fixed bg-light-50;
}
</style>
